package com.zzyl.serve.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.common.core.redis.RedisCache;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.service.INursingPlanService;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    @Autowired
    private INursingProjectPlanService iNursingProjectPlanService;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Transactional
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        // 1.获取护理计划基本信息
        //QueryWrapper<NursingPlan> queryWrapper = new QueryWrapper<>();
        //queryWrapper.eq("id", id).orderByDesc("sort_no");
        NursingPlan nursingPlan = this.getById(id);
        // 2.获取当前护理计划关联的护理项目列表
        QueryWrapper<NursingProjectPlan> nursingProjectPlanQueryWrapper = new QueryWrapper<>();
        nursingProjectPlanQueryWrapper.eq("plan_id", id);
        List<NursingProjectPlan> nursingProjectPlans = nursingProjectPlanMapper.selectList(nursingProjectPlanQueryWrapper);
        // 3.封装到VO中并返回
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);
        ArrayList<NursingProjectPlanVo> nursingProjectPlanVos = new ArrayList<>();
        if (nursingProjectPlans != null && nursingProjectPlans.size() > 0) {
            for (NursingProjectPlan nursingProjectPlan : nursingProjectPlans){
                NursingProjectPlanVo nursingProjectPlanVo = new NursingProjectPlanVo();
                BeanUtils.copyProperties(nursingProjectPlan, nursingProjectPlanVo);
                nursingProjectPlanVo.setProjectId(String.valueOf(nursingProjectPlan.getProjectId()));
                nursingProjectPlanVo.setExecuteCycle(String.valueOf(nursingProjectPlan.getExecuteCycle()));
                nursingProjectPlanVo.setExecuteFrequency(Long.valueOf(nursingProjectPlan.getExecuteFrequency()));
                nursingProjectPlanVo.setExecuteTime(nursingProjectPlan.getExecuteTime());
                nursingProjectPlanVos.add(nursingProjectPlanVo);
            }
        }
        nursingPlanVo.setProjectPlans(nursingProjectPlanVos);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        QueryWrapper<NursingPlan> nursingPlanQueryWrapper = new QueryWrapper<>();
        // 动态添加 plan_name 模糊查询条件
        if (nursingPlan.getPlanName() != null && !nursingPlan.getPlanName().isEmpty()) {
            nursingPlanQueryWrapper.like("plan_name", nursingPlan.getPlanName());
        }
        // 动态添加 status 精确查询条件
        if (nursingPlan.getStatus() != null) {
            nursingPlanQueryWrapper.eq("status", nursingPlan.getStatus());
        }
        nursingPlanQueryWrapper.orderByAsc("sort_no");
        List<NursingPlan> nursingPlans = nursingPlanMapper.selectList(nursingPlanQueryWrapper);

        return nursingPlans;
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDTO 护理计划及项目
     * @return 结果
     */
    @Transactional
    @Override
    public int insertNursingPlan(NursingPlanDto nursingPlanDTO) {
        clearCache();

//        NursingPlan nursingPlan = new NursingPlan();
//        BeanUtils.copyProperties(nursingPlanDTO, nursingPlan);
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDTO, NursingPlan.class);
        boolean result1 = save(nursingPlan);
        // 批量插入项目计划
        List<NursingProjectPlan> nursingProjectPlans = nursingPlanDTO.getProjectPlans();
        boolean result2 = false;
        if (nursingProjectPlans.get(0).getProjectId()!= null) {//默认传入执行频次1，优化判断是否传入projectId
            for (NursingProjectPlan projectPlan : nursingProjectPlans) {
                projectPlan.setPlanId(nursingPlan.getId());
            }
            result2 = iNursingProjectPlanService.saveBatch(nursingProjectPlans);
        }
        if (result1 || result2) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int updateNursingPlan(NursingPlanDto nursingPlanDto) {
        clearCache();

        // 1.修改护理计划关联的护理项目列表
//        NursingPlan nursingPlan = new NursingPlan();
//        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);

        boolean result1 = updateById(nursingPlan);

        // 2.删除当前护理计划关联的所有护理项目
        nursingProjectPlanMapper.delete(new QueryWrapper<NursingProjectPlan>()
                .eq("plan_id", nursingPlan.getId()));

        boolean result2 = false;
        //  3.批量保存护理计划新关联的护理项目
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if (projectPlans != null && projectPlans.size() > 0) {
            result2 = iNursingProjectPlanService.saveBatch(projectPlans);
        }
        if (result1 || result2) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 删除护理计划
     *
     * @param id 需要删除的护理计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteNursingPlanaAndProjectById(Long id) {
        clearCache();
        //1,删除护理计划
        int result1 = nursingPlanMapper.deleteById(id);

        //2,删除护理计划关联的护理项目
        int result2 = nursingProjectPlanMapper.delete(new QueryWrapper<NursingProjectPlan>()
                .eq("plan_id", id));

        return result1 + result2;
    }

    /**
     * 查询所有护理计划
     */
    @Override
    public List<NursingPlan> getAll() {

        //从缓存中获取
        List<NursingPlan> nursingPlans = redisCache.getCacheList(CacheConstants.NURSING_PLAN_ALL_KEY);
        if (CollUtil.isEmpty(nursingPlans)){
            LambdaQueryWrapper<NursingPlan> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(NursingPlan::getStatus, 1);
            nursingPlans = this.list(queryWrapper);
            redisCache.setCacheList(CacheConstants.NURSING_PLAN_ALL_KEY, nursingPlans);
        }
        return nursingPlans;
    }

    /**
     * 清理缓存
     */
    private void clearCache(){
        redisCache.deleteObject(CacheConstants.NURSING_PLAN_ALL_KEY);
    }

//    /**
//     * 删除护理计划信息
//     *
//     * @param id 护理计划主键
//     * @return 结果
//     */
//    @Override
//    public int deleteNursingPlanById(Long id) {
//        return removeById(id) ? 1 : 0;
//    }
}
